package com.ajun.listener.topic;

import com.ajun.common.rabbit.QueueConstants;
import com.ajun.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Email RabbitMQ 消费者
 *
 * @author AJun
 * @date 2021-07-14
 */
@Slf4j
@Component
@RabbitListener(
    bindings = @QueueBinding(
        value = @Queue(value = QueueConstants.EMAIL_TOPIC_QUEUE, declare = "true", autoDelete = "false"),
        exchange = @Exchange(value = QueueConstants.ORDER_TOPIC_EXCHANGE, type = ExchangeTypes.TOPIC),
        key = QueueConstants.EMAIL_TOPIC_KEY
    )
)
public class TopicEmailListener {

    @Resource
    private EmailService emailService;

    @RabbitHandler
    public void recvMessage(String message) {
        log.info("Email 接收到订单信息(topic),orderId => {}", message);
        // 发送邮件给用户
        log.info(emailService.sendEmail("1", message));
    }
}
